var oprateUrl = DOMAIN + '/v1/accounts' + isLogin;
$(document).ready(function () {
	loadTable();
	getCheList($('#roleId'), '/v1/roles' + isLogin, 'roleId');
	getSelList($("#userType"),"user_type");
});

$("#Btn-insert").click(function () {
	//清空表单
	$("#form-modal")[0].reset();
	$("#roleId input[name='roleId']").removeAttr("checked");
});

//查询
$("#searchBtn").click(function () {
	//新查询参数
	function queryParamNew(params) {
		var params = {};
		params.userName = $("#userName").val();
		params.loginName = $("#loginName").val();
		params.telephone = $("#telephone").val();
		//params.isUsed = $("input[name='isUsed']:checked").val();
		return params;
	};
	$('#table').bootstrapTable(
		'refreshOptions', {
			url: oprateUrl,
			//查询参数
			queryParams: queryParamNew
		}
	);
});

function loadTable() {
	$('#table').bootstrapTable({
		url: oprateUrl,
		method: 'get',
		dataType: "json",
		contentType: "application/x-www-form-urlencoded",
		toolbar: '#toolbar',
		striped: true,
		cache: false,
		pagination: true,
		sortable: false,
		sortOrder: "asc",
		showRefresh: false,
		showColumns: false,
		minimumCountColumns: 4,
		search: false,
		queryParams: queryParams,
		toolbar: '#exampleTableEventsToolbar',
		sidePagination: "client",
		pageNumber: 1,
		pageSize: 50,
		pageList: [50, 100],
		strictSearch: true,
		clickToSelect: true,
		cardView: false,
		detailView: false,
		columns: [{
			field: 'userName',
			title: "真实姓名",
			align: "center",
			valign: "middle"
		},{
			field: "loginName",
			title: "登录名",
			align: "center",
			valign: "middle"
		},{
			field: "contactPhone",
			title: "联系电话",
			align: "center",
			valign: "middle"
		},{
			field: 'telephone',
			title: "手机号",
			align: "center",
			valign: "middle"
		}, {
			field: "email",
			title: "邮箱",
			align: "center",
			valign: "middle"
		},{
			field: 'qq',
			title: "QQ",
			align: "center",
			valign: "middle"
		},{
			field: 'userTypeName',
			title: "客户类型",
			align: "center",
			valign: "middle"
		}
		,{
			field: 'userType',
			title: "客户类型代码",
			align: "center",
			valign: "middle",
			visible: false
		},{
			field: 'department',
			title: "部门",
			align: "center",
			valign: "middle"
		}, {
			field: "workAddress",
			title: "企业地址",
			align: "center",
			valign: "middle"
		},/*{
			field: 'authCode',
			title: "授权码",
			align: "center",
			valign: "middle"
		},*/{
			field: "",
			title: "操作",
			align: "center",
			valign: "middle",
			events: funOprate,
			formatter: function (value, row, index) {
				return '<button data-toggle="modal" id="' + row.id + '" data-edit="div-modal" data-target="#div-modal" class="btn btn-primary btn-sm edit" onclick="loadRole(this)">&emsp;编辑</button> <button data-del="' + oprateUrl + '" class="btn btn-primary btn-sm del">&emsp;删除</button>';
			}
		}],
		onLoadSuccess: function () {
			console.log("加载成功");
		},
		onLoadError: function () {
			console.log("加载数据失败");
		},
		//>>>>>>>>>>>>>>导出excel表格设置
		showExport: true,
		showExport: true,
		//是否显示导出按钮
		buttonsAlign: "left",
		//按钮位置
		exportDataType: 'all',
		//导出的方式 all全部 selected已选择的  basic', 'all', 'selected'.
		Icons: 'glyphicon glyphicon-export',
		exportTypes: ['excel', 'doc', 'xlsx', 'txt',],
		//导出文件类型
		exportOptions: {       //ignoreColumn: [0,1],  //忽略某一列的索引

			fileName: '导出数据表',
			//文件名称设置
			worksheetName: 'sheet1',
			//表格工作区名称
			tableName: '导出数据表',
			// excelstyles: ['background-color', 'color', 'font-size', 'font-weight'], 设置格式

		},
		//导出excel表格设置<<<<<<<<<<<<<<<<
	});
	//查询参数
	function queryParams(params) {
		var params = {};
		return params;
	};
};

//保存
function funSave(data) {
	var id = $("#id").val();
	var url = "";
	var type = "";
	if (!ifNull(data)) {
		return;
	}
	data = JSON.stringify(data);
	console.log(data);
	if (isNotEmpty(id)) {//修改
		type = "PATCH";
	} else {//新增
		type = "POST";
	}
	$.ajax({
		type: type,
		url: oprateUrl,
		data: data,
		async: false,
		dataType: 'JSON',
		contentType: "application/JSON; charset=utf-8",
		success: function (result) {
			var status = result.status;
			if (status == 201) {
				alert("登录名已存在！");
			};
			if (status == 200) {
				alert("保存成功");
				$('.close').click();
				document.getElementById("form-modal").reset();
				$("#id").val("");
				$("#table").bootstrapTable("refresh", {
					silent: true //静态刷新
				});
			}
		},
		error: function () {
			alert("修改失败！");
		}
	});
};
//保存
$("#btn-save").click(function () {
	var RoleListYes = [];
	var RoleListNo = [];
	$('input[name="roleId"]:checked').each(function () {
		RoleListYes.push($(this).val());
	});
	$('input[name="roleId"]').not("input:checked").each(function () {
		RoleListNo.push($(this).val());
	});
	var data = {};
	data.id = $("#id").val();
	data.userName = $("#userNameAdd").val();
	data.loginName = $("#loginNameAdd").val();
	data.password = $("#password").val();
	data.contactPhone = $("#contactPhone").val();
	data.telephone = $("#telephoneAdd").val();
	data.qq = $("#qq").val();
	data.email = $("#email").val();
	data.userType = $("#userType option:checked").val();
	data.department = $("#department").val();
	data.workAddress = $("#workAddress").val();
	data.authCode = $("#authCode").val();
	data.roleListYes = RoleListYes;
	data.roleListNo = RoleListNo;
	funSave(data);
});

//关闭
$("#btn-close").click(function () {
	document.getElementById("form-modal").reset();
	$("#id").val("");
});



function loadRole(e) {
	var id = $(e).attr('id');
	$.ajax({
		type: "GET",
		url: DOMAIN + '/v1/selectRoleById/' + id + isLogin,
		async: false,
		success: function (result) {
			$("#roleId input[name='roleId']").removeAttr("checked");
			var needCompareId = result.data;
			for (var i = 0; i < needCompareId.length; i++) {
				//
				$('#roleId input').each(function () {
					var compareId = $(this).val();
					if (compareId == needCompareId[i].roleId) {
						$(this).prop('checked', true)
					}
				});
				//
			}

		},
		error: function () {
			alert("修改失败！");
		}
	});
}


function ifNull(data) {
	if (data.userName == '' || data.userName == null) {
		alert("用户名称不能为空！");
		return false;
	}
	if (data.loginName == '' || data.loginName == null) {
		alert("登录名不能为空！");
		return false;
	}
	if (data.password == '' || data.password == null) {
		alert("登录密码不能为空！");
		return false;
	}
	if (data.telephone == '' || data.telephone == null) {
		alert("手机号不能为空！");
		return false;
	}
	if (data.userType == '' || data.userType == null) {
		alert("用户类型不能为空！");
		return false;
	}
	
	return true;
}